// pages/newAddress/newAddress.js
import request from '../../utils/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mode: '',
    id: 0,
    userInfo: {
      sex: '0',
      phone: '',
      longitude: '',
      latitude: '',
      addr: '请选择送餐地址',
      desc: '',
      receiver: '',
      istop: '0',
    },
  },
  onLoad: function (options) {
    console.log(this.options)
    if (this.options.mode === 'PUT') {
      this.setData({
        mode: options.mode,
        id: options.id,
      })
      request(
        {
          url: 'http://120.78.172.212:9001/api/address?limit=100&&page=1',
        },
        (res) => {
          console.log(res.data.rows)
          console.log(this.data.id)
          let user = res.data.rows.find((x) => {
            return x.id == this.data.id
          })
          let userInfo = this.data.userInfo
          for (let x in userInfo) {
            userInfo[x] = user[x]
          }
          userInfo.id = parseInt(this.data.id)
          this.setData({
            userInfo,
          })
        }
      )
    }
    this.setData({
      mode: options.mode,
    })
  },
  // 打开地图
  openMap() {
    wx.authorize({
      scope: 'scope.userLocation',
      success: (e) => {
        wx.getLocation({
          type: 'gcj02',
          success: (location) => {
            console.log(location)
            // let locationString=location.latitude+','+location.longitude
            // wx.request({
            //   url: 'https://apis.map.qq.com/ws/geocoder/v1',
            //   data:{
            //     "key":"FMOBZ-R4NK3-7SH3S-325H2-G5HSK-HEFCV",
            //     location:locationString
            //   },
            //   method:"GET",
            //   success:(res)=>{
            //     console.log(res);
            //   }
            // })
            wx.chooseLocation({
              success: (res) => {
                console.log(res)
                let userInfo = this.data.userInfo
                userInfo.latitude = res.latitude
                userInfo.longitude = res.longitude
                userInfo.addr = res.address
                this.setData({
                  userInfo,
                })
              },
            })
          },
        })
      },
    })
  },
  // input监听
  handlerInputChange(e) {
    let name = e.currentTarget.dataset.name
    let value = e.detail.value
    let userInfo = this.data.userInfo
    userInfo[name] = value
    this.setData({
      userInfo,
    })
  },
  // 修改性别
  changeSex(e) {
    console.log(e)
    let sex = e.currentTarget.dataset.sex
    console.log(sex)
    let userInfo = this.data.userInfo
    userInfo.sex = sex
    this.setData({
      userInfo,
    })
  },
  // 提交表单
  commit() {
    let mode = this.data.mode
    request(
      {
        url: 'http://120.78.172.212:9001/api/address',
        method: mode,
        data: this.data.userInfo,
      },
      (res) => {
        if (res.code === 200) {
          wx.navigateTo({
            url: '../chooseAddress/chooseAddress',
            success: (result) => {},
            fail: () => {},
            complete: () => {},
          })
        }
      }
    )
  },

})
